////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2003-2006 Adobe Macromedia Software LLC and its licensors.
// All Rights Reserved.
// The following is Sample Code and is subject to all restrictions on such code
// as contained in the End User License Agreement accompanying this product.
// If you have received this file from a source other than Adobe,
// then your use, modification, or distribution of it requires
// the prior written permission of Adobe.
//
////////////////////////////////////////////////////////////////////////////////
package samples.flexstore {

	import com.metasoft.flex.MetasoftULC;
	
	import flash.net.SharedObject;
	
	import mx.collections.ArrayCollection;
	[Bindable]
	public class ProductFilter {
	
		public var isGenres:Boolean ;
	    public var count:int;
	    public var series:String;
	    public var minPrice:Number;
	    public var maxPrice:Number;
	    public var triband:Boolean;
	    public var camera:Boolean;
	    public var video:Boolean;
	    public var isTrue:Boolean

	    public var category:String;
		public var contentType:String; 
		public var intention:String;
		public var audience:String;
	    public var atmosphere :Array;
		public var contentAlert:String;
		public var format:String;
		public var origination : String;
		
		public var keyword : String;
		
		public var state : String;

	    private var genreList:ArrayCollection;
	    
	    public function ProductFilter() {
	        super();
	    }
	    
	    public function accept(product:Product):Boolean {
	
			if (product != null && product.genreArray != null) {
	
				var i : int = 0;
				genreList = new ArrayCollection();
				/*if (minPrice == 0 && maxPrice == 10) {
					return true;
			 	}*/
				if ((category != null && category != "") || 
					(atmosphere != null && atmosphere.length != 0) || 
					audience  != null || format != null || 
					contentType != null || intention != null ||
					origination != null || contentAlert != null ||
					(keyword != null && keyword != "") ||
					minPrice >= 0 && maxPrice <= 10) {

					if (/*(category != null && category != "") || 
						(atmosphere != null && atmosphere.length != 0) || 
						audience  != null || format != null || 
						contentType != null || intention != null ||
						origination != null || contentAlert != null*/
						minPrice == 0 && maxPrice == 10) {

						for (i = 0; i < product.genreArray.length; i++) {
					 		genreList.addItem(product.genreArray[i]);
					 	}
					}
			    	isGenres = true;
			    	if (keyword != null && keyword != "") { 
			    	
			    		if (keyword.match(product.keyword)) {
			    			return true;
			    		}
			    	} else if ( genreList != null &&  genreList.length > 0){
			    		
			  			for (var x:int = 0; x <  genreList.length; x++){
	
							if (category != null && category != "") {
	
						   		if(genreList[x].href.toString().match("urn:tva:metadata:cs:ContentCS:2005")){

			    					if (genreList[x].name.stringValue.match(category)){

			    						return true;
			    						break;
			    			 		}
		    			 		}
	    			 		} else if (atmosphere != null){

			    		 		if(genreList[x].href.toString().match("urn:tva:metadata:cs:AtmosphereCS:2005")){

			 			    		for (var i:int =0 ; i < atmosphere.length ; i ++){

										if (atmosphere[i] == null ||
											atmosphere[i] == "") {
												continue;
										}
			    						if (genreList[x].name.stringValue.match(atmosphere[i])){

			    							return true;
			    							break;
			    						}
			    			 		}
			    			 	}
			    			 } else if(audience != null){		 

		    			 		 if(genreList[x].href.toString().match("urn:tva:metadata:cs:IntendedAudienceCS:2005")){

									if (genreList[x].name.stringValue.match(audience)){

		    							isTrue =  true;
					    				return true;
		    							break;
		    			 			}
			    			 	}
			    			 } else if(format != null){

			    			 	if(genreList[x].href.toString().match("urn:tva:metadata:cs:FormatCS:2005")){

		    						if (genreList[x].name.stringValue.match(format)){

		    							isTrue =  true;
		    							return true;
		    							break;
	    			 				}
		    			 		}
		    			 	} else if(contentType != null){

		    			 		 if(genreList[x].href.toString().match("urn:tva:metadata:extended:cs:ContentTypeCS:2005")){

		    						if (genreList[x].name.stringValue.match(contentType)){

		    							isTrue =  true;
		    							return true;
		    							break;
		    			 			}
			    			 	}
			    			 } else if(intention != null){

		    			 		 if(genreList[x].href.toString().match("urn:tva:metadata:cs:IntentionCS:2005")){

		    						if (genreList[x].name.stringValue.match(intention)){

		    							isTrue =  true;
	    								return true;
		    							break;
		    			 			}
	    			 			}
		    			 	} else if(contentAlert != null){

	    			 		 	if(genreList[x].href.toString().match("urn:tva:metadata:cs:ContentAlertCS:2005")){

		    						if (genreList[x].name.stringValue.match(contentAlert)){

		    							isTrue =  true;
	    								return true;
		    							break;
		    			 			}
		    			 		}
		    			 	} else if(origination != null){

	    			 		 	if(genreList[x].href.toString().match("urn:tva:metadata:cs:OriginationCS:2005")){

		    						if (genreList[x].name.stringValue.match(origination)){

		    							isTrue =  true;
	    								return true;
		    							break;
		    			 			}
		    			 		}
		    			 	} else if (minPrice == 0 && maxPrice == 10) {

		    			 		return true;
		    			 		break;
		    			 	} else {
	    			 			return false;
	    			 		}
				 		}
			 		} else if(minPrice >= 0 && minPrice <= 10 && maxPrice >= 0 && maxPrice <= 10) {

						if (minPrice == 0 && maxPrice == 10) {
							return true;
						}
						var so:SharedObject = SharedObject.getLocal(MetasoftULC.LoggedInUser);
						var reviewArray : Array = so.data.review;
						if (reviewArray != null && reviewArray.length > 0) {

							var i : int = 0;
							for (i = 0; i < reviewArray.length; i++) {

								if (reviewArray[i] == null) {
									continue;
								}
								var reviewCrid : String = reviewArray[i].reviewCrid;
								if (reviewCrid == null) {
									continue;
								}
								if (reviewCrid == product.id) {

									var reviewRating : Array = reviewArray[i].ratingValue;
									if (reviewRating != null && reviewRating.length > 0) {

										var j : int = 0;
										for (j = 0; j < reviewRating.length; j++) {

											if (reviewRating[j] == null) {
												continue;
											}
											var reviewRatingNumber : Number = Number(reviewRating[j]); 
											if (minPrice <= reviewRatingNumber && maxPrice >= reviewRatingNumber) {

												isTrue =  true;
			    								return true;
				    							break;
											}
										}
									}
								}
							}
						}
    			 	}
		 		}else{
			    	isGenres = false;
		 		}
		        if (!isTrue && !isGenres){
	
			        var price : Number = 0.0;
			       	if (product.price == null) {
			       		return false;
			       	} else {
			
			       		var priceString : String = product.price;
			       		price = Number(priceString.substr(1, priceString.length));
			       	}
			        if (minPrice > price || maxPrice < price)
			            return false;
			        if (series != "All Series" && series != product.series)
			            return false;
			        if (triband && !product.triband)
			            return false;
			        if (camera && !product.camera)
			            return false;
			        if (video && !product.video)
			            return false;
//				    if (returnString == false)
//			       		return false;
		        }
		        if (isGenres){
		        	return false;
		        } else {
		        	  return true;
		        }
	 		}
	 		return false;
	    }
	}
}
